package com.gai.shop.web.controller;

import com.gai.shop.common.Const;
import com.gai.shop.entity.Product;
import com.gai.shop.entity.Type;
import com.gai.shop.service.ProductService;
import com.gai.shop.service.TypeService;
import com.gai.shop.service.impl.ProductServiceImpl;
import com.gai.shop.service.impl.TypeServiceImpl;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@MultipartConfig(maxFileSize = 1024 * 1024 * 100, maxRequestSize = 1024 * 1024 * 200)
public class AdminProductController extends BaseController {
    private final TypeService TS = new TypeServiceImpl();
    private final ProductService PS = new ProductServiceImpl();

    /**
     * 获取商品类别数据 动态查询
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String getGoodsType(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String tname = request.getParameter("tname");
        //向业务层访问
        List<Type> allByKey = TS.getAllByKey(tname);
        request.setAttribute(Const.AdminProductConst.GOODS_TYPE_LIST,allByKey);
        String type = request.getParameter("type");
        if (type != null && type.equals("1")){
            return  Const.FORWARD + "/admin/addGoods.jsp";
        }else {
            return  Const.FORWARD + "/admin/showGoodsType.jsp";
        }
    }

    /**
     *
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public String addPro(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        //先获取普通数据
        Map<String, String[]> parameterMap = request.getParameterMap();
        //声明一个封装的实体类
        Product p = new Product();
        /*
         * 自己设置日期的封装规则
         * BeanUtils本身无法自己去转换String==>java.util.Date类型
         * 所以需要使用下方的日期转换规则，自己去设置规则
         */
//        DateConverter c = new DateConverter();
//        c.setPattern("yyyy-MM-dd");
        //把规则进行注册
//        ConvertUtils.register(c, Date.class);
        //BeanUtils进行封装 普通数据的封装
        BeanUtils.populate(p,parameterMap);
        //文件上传
        //获取上传的文件
        try {
            //Part 看成上传的文件本身
            Part part = request.getPart("pimage");
            if (part != null){
                String fileName = part.getSubmittedFileName();//获取文件名
                //获取项目真实路径
                //D:\IDEA\myshop\web\image
                String realPath = request.getServletContext().getRealPath("/image");
                //封装到product对象===>保存图片路径到数据库
                p.setPimage("image/" + fileName);
                //把图片写出到我们的图片文件夹下面去
                part.write(realPath + "\\" + fileName);
            }
            //访问业务层保存数据到数据库
            PS.addPro(p);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ServletException e) {
            e.printStackTrace();
        }
        return  Const.REDIRECT + "/admin_pro?method=getProByKey";
    }

    /**
     *
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String addType(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String tname = request.getParameter("tname");
        String tinfo = request.getParameter("tinfo");
        TS.addType(tname,tinfo);
        return Const.REDIRECT + "/admin_pro?method=getGoodsType";
    }

    /**
     * 获取商品数据 动态查询
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String getProByKey(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取动态查询使用的条件参数
        String pname = request.getParameter("pname");
        String ptime = request.getParameter("ptime");
        //访问业务层
        List<Product> proListByKey = PS.getProListByKey(pname, ptime);
        //使用request进行存值
        request.setAttribute(Const.PRO_LIST,proListByKey);
        return Const.FORWARD + "/admin/showGoods.jsp";
    }
}
